

import 'adapter/dio_adapter.dart';
import 'base_request.dart';
import 'hi_error.dart';
import 'lp_net_adapter.dart';

class LpNet {
  LpNet._privateConstructor();

  static final LpNet _instance = LpNet._privateConstructor();

  static LpNet get instance {
    return _instance;
  }

  Future fire(BaseRequest request) async {
    BaseResponse? response;
    var error;
    try {
      response = await send(request);
    } on LpNetError catch (e) {
      error = e;
      response = e.data;
      response?.errorMsg = e.message;
    } catch (e) {
      error = e;
    }

    if (response == null) {
      print(error);
      return LpNetError(-1, "response is null", "null");
    }

    var result = response.data;
    var status = response.errorCode;
    switch (status) {
      case 0:
        print("请求成功：$result");
        return result;
      case -1:
        throw NeedLogin();
      default:
        throw LpNetError(status, response.errorMsg, result);
    }
  }

  Future<dynamic> send<T>(BaseRequest request) async {
    print("请求地址："+request.url().toString());
    // LpNetAdapter adapter = MockAdapter();
    LpNetAdapter adapter = DioAdapter();
    return adapter.send(request);
  }
}
